package com.lzk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.common.CustomerException;
import com.lzk.common.R;
import com.lzk.dto.SetmealDto;
import com.lzk.mapper.SetmealMapper;
import com.lzk.pojo.Category;
import com.lzk.pojo.Setmeal;
import com.lzk.pojo.SetmealDish;
import com.lzk.service.CategoryService;
import com.lzk.service.SetmealDishService;
import com.lzk.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    private SetmealService setmealService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private SetmealDishService setmealDishService;

    @Override
    public R<Page<SetmealDto>> setmealPage(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(null != name, Setmeal::getName, name);
        setmealService.page(setmealPage, lqw);
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, dtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            setmealDtoList.add(setmealDto);
        }
        dtoPage.setRecords(setmealDtoList);
        return R.success(dtoPage);
    }

    @Override
    @Transactional
    public R<String> saveSetMeal(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealService.save(setmeal);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        List<SetmealDish> dishesList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            dishesList.add(setmealDish);
        }
        setmealDishService.saveBatch(dishesList);
        return R.success("添加成功");
    }

    @Override
    @Transactional
    public R<SetmealDto> showSetmealById(Long id) {
        Setmeal setmeal = setmealService.getById(id);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishService.list(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return R.success(setmealDto);
    }

    @Override
    @Transactional
    public R<String> updateWithDishes(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        setmealService.updateById(setmeal);
        Long setmealId = setmeal.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(lqw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @Override
    @Transactional
    public R<String> stopSaleByIds(int status, List<Long> ids) {
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        List<Setmeal> setmealList = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
            setmealList.add(setmeal);
        }
        setmealService.updateBatchById(setmealList);
        return R.success("状态修改成功");
    }

    @Override
    @Transactional
    public R<String> delete(List<Long> ids) throws CustomerException {
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId, ids);
        List<Setmeal> setmealList = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus() == 1) {
                throw new CustomerException("商品售卖中，请停售后再删除");
            }
        }
        setmealDishService.remove(lqw);
        setmealService.removeByIds(ids);
        return R.success("删除成功");
    }

    @Override
    public R<List<Setmeal>> listAll(Setmeal setmeal) {
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null,Setmeal::getCategoryId,categoryId)
                    .eq(status != null,Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
